package com.miyake.loveviewweb.client.community;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.loveviewweb.client.dialog.BasicDialog;
import com.miyake.loveviewweb.client.dialog.DialogHandler;
import com.miyake.loveviewweb.client.dialog.IDialogPanel;
import com.miyake.loveviewweb.client.dialog.UploadDialog;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.shared.model.UserModel;

public class UserPropertyPanel extends Composite implements IDialogPanel {
    private VerticalPanel contentPanel;
    private VerticalPanel imagePanel;
    private Image image;
    private boolean editable;
    private UserModel userModel;
    
    public  UserPropertyPanel(UserModel userModel, boolean editable) {
        this.editable = editable;
        this.userModel = userModel;
        ScrollPanel scrollPanel = new ScrollPanel();
        initWidget(scrollPanel);
        VerticalPanel vpanel = new VerticalPanel();
        vpanel.setWidth("100%");
        scrollPanel.add(vpanel);               
        scrollPanel.setWidth("100%");
        
        MenuBar menuBar = new MenuBar();
        MenuItem menuItem = new MenuItem("", new Command() {

            public void execute() {

            }
            
        });
        menuBar.addItem(menuItem);
        HorizontalPanel hpanel = new HorizontalPanel();
        
        vpanel.add(menuBar);
        vpanel.add(hpanel);
       
        contentPanel = new VerticalPanel();
        imagePanel = new VerticalPanel();
        
        image = new Image(userModel.getImageUrl());
        image.setSize("130px", "150px");
        imagePanel.add(image);
        hpanel.add(imagePanel);
        hpanel.add(contentPanel);
        
        initialize();
    }

    private void initialize() {
        if (editable) {
            initEditable();
        }
        else {
            initReadonly();
        }
    }

    private void initReadonly() {
        contentPanel.add(new Label("First Name:" + userModel.getFirstName()));
        contentPanel.add(new Label("Last Name:" + userModel.getLastName()));
        contentPanel.add(new HTML("Email:" + userModel.getEmail()));
        contentPanel.add(new Label("Job:"));
        contentPanel.add(new Label("Age:"));
        contentPanel.add(new Label("Gender:"));
        contentPanel.add(new Label("Nationality:"));
        contentPanel.add(new Label("Speciality:"));
    }

    private void initEditable() {
        contentPanel.add(new Label("First Name:"));
        final TextBox textFirstName = new TextBox();
        textFirstName.setText(userModel.getFirstName());
        contentPanel.add(textFirstName);
        
        contentPanel.add(new Label("Last Name:"));
        final TextBox textLastName = new TextBox();
        textLastName.setText(userModel.getLastName());
        contentPanel.add(textLastName);
        
        contentPanel.add(new Label("Email:"));
        contentPanel.add(new Label(userModel.getEmail()));
        
        contentPanel.add(new Button("Update", new ClickHandler() {

            public void onClick(ClickEvent event) {
                userModel.setFirstName(textFirstName.getText());
                userModel.setLastName(textLastName.getText());
                DataService.Util.getInstance().updateUser(userModel, new AsyncCallback<Void>() {

                    public void onFailure(Throwable caught) {
                        // TODO Auto-generated method stub
                        
                    }

                    public void onSuccess(Void result) {
                        // TODO Auto-generated method stub
                        
                    }
                    
                });
            }
            
        }));
        
        imagePanel.add(new Button("Upload", new ClickHandler() {
            public void onClick(ClickEvent event) {
                new BasicDialog(new UploadDialog("", "userImage")) {
                    @Override
                    protected boolean isOkVisible() {
                        return false;
                    }

                    @Override
                    protected void onOK(Composite composite) {
                        updateImage();
                    }
                    
                }.showDialog();
                
            }
        }));
    }
    
    private void updateImage() {
        DataService.Util.getInstance().getUser(userModel.getKey(), new AsyncCallback<UserModel>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(UserModel result) {
                userModel = result;
                image.setUrl(result.getImageUrl());
            }
            
        });
    }

    public void setDialogHandler(DialogHandler handler) {
        // TODO Auto-generated method stub
        
    }

    public Composite getComposite() {
        return this;
    }
}
